package com.example.hospital.controller;

import com.example.hospital.common.ApiResponse;
import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.entity.IndicatorResult;
import com.example.hospital.service.IndicatorResultService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/indicator-results")
public class IndicatorResultController {
    private final IndicatorResultService indicatorResultService;
    public IndicatorResultController(IndicatorResultService indicatorResultService) { this.indicatorResultService = indicatorResultService; }
    /**
     * 创建指标结果
     * @param result
     * @return
     */
    @PostMapping
    public ApiResponse<IndicatorResult> create(@RequestBody IndicatorResult result) {
        return ApiResponse.success(indicatorResultService.create(result));
    }
    /**
     * 更新指标结果异常标志与医生备注
     * @param id
     * @param abnormalFlag
     * @param doctorComment
     * @return
     */
    // 扩展：允许同时更新结果值、异常标记与医生备注；保持兼容仅更新异常标记与备注的旧调用
    @PutMapping("/{id}")
    public ApiResponse<Boolean> update(@PathVariable("id") Long id,
                                       @RequestParam(name = "abnormalFlag", required = false) Boolean abnormalFlag,
                                       @RequestParam(name = "doctorComment", required = false) String doctorComment,
                                       @RequestParam(name = "resultValue", required = false) String resultValue) {
        if (resultValue != null) {
            return ApiResponse.success(indicatorResultService.updateValueFlagComment(id, resultValue, abnormalFlag, doctorComment));
        }
        return ApiResponse.success(indicatorResultService.updateFlagAndComment(id, abnormalFlag, doctorComment));
    }
    /**
     * 分页查询指标结果（按样本ID、指标ID、异常标志筛选）
     * @param sampleId
     * @param indicatorId
     * @param abnormalFlag
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    public ApiResponse<PageResponse<IndicatorResult>> list(@RequestParam(name = "sampleId", required = false) Long sampleId,
                                                           @RequestParam(name = "indicatorId", required = false) Long indicatorId,
                                                           @RequestParam(name = "abnormalFlag", required = false) Boolean abnormalFlag,
                                                           @RequestParam(name = "page", defaultValue = "1") int page,
                                                           @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(indicatorResultService.list(sampleId, indicatorId, abnormalFlag, page, size));
    }
    /**
     * 分页查询用户或订单下的指标结果（按异常标志筛选）
     * @param userId
     * @param orderId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    public ApiResponse<PageResponse<IndicatorResult>> listByUserOrOrder(@RequestParam(name = "userId", required = false) Long userId,
                                                                        @RequestParam(name = "orderId", required = false) Long orderId,
                                                                        @RequestParam(name = "page", defaultValue = "1") int page,
                                                                        @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(indicatorResultService.listByUserOrOrder(userId, orderId, page, size));
    }
}